﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common;
using System.Collections;

namespace Model
{
     public class CoreDAO
    {
         public void BuildXMLCode(Dictionary<string, exFullInfoForBuildChild> dic,string path)
         {
             try
             {
                 XmlDocument doc = new XmlDocument();

                 //此路径也是最后生成文件的路径
                 //string path = PubConstant.ControlXmlTemplatePath;
                 //path = String.Format("{0}BuildTest2.xml", path);

                 //定义XML声明
                 XmlNode dn = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                 doc.AppendChild(dn);

                 //定义根节点-根节点名documentRoot
                 XmlNode documentRoot = doc.CreateElement("Document");
                 doc.AppendChild(documentRoot);

                 //读取代码生成模板并添加
                 string codeTampPath = String.Format("{0}CSharpBuildTemplate.xml", PubConstant.TemplatePath);
                 XmlDocument codeDoc = new XmlDocument();
                 codeDoc.Load(codeTampPath);
                 XmlNode _node2 = codeDoc.DocumentElement.ChildNodes[0];
                 XmlNode _nodeCopy = doc.CreateNode(XmlNodeType.Element, _node2.Name, "");
                 _nodeCopy.InnerXml = _node2.InnerXml;
                 documentRoot.AppendChild(_nodeCopy);


                 #region 基底节点-节点名BaseControl
                 //添加基底节点
                 exFullInfoForBuildChild documentEX = dic["Document"];
                 XmlElement BaseControl = doc.CreateElement("Object");
                 BaseControl.SetAttribute("name",documentEX.BuildName);
                 BaseControl.SetAttribute("assembly",documentEX.ControlFullName);
                 BaseControl.SetAttribute("type",documentEX.BaseType.ToString());
                 BaseControl.SetAttribute("children","Controls");
                 

                 //添加基底节点的属性
                 SortHelper.SortList<eProperty>("PropertyType", "ASC", documentEX.PropertyList);//排序
                 foreach (eProperty ep in documentEX.PropertyList)
                 {

                     XmlElement xe = null;
                     if (ep.ValueTypeFullName == enumPropertyType.Data.ToString())
                     {
                         xe = doc.CreateElement("Data");
                         xe.SetAttribute("Type", "Controls");
                         StringBuilder ss1 = new StringBuilder();
                         foreach (string s in ep.Value.Split(','))
                         {
                             ss1.Append(s);
                             ss1.Append(",");
                         }
                         xe.InnerText = ss1.ToString().Substring(0, (ss1.ToString().Length - 1));

                     }
                     else
                     {
                         xe = doc.CreateElement(ep.PropertyType.ToString());
                         xe.SetAttribute("name", ep.Name);
                         xe.SetAttribute("valueType", ep.ValueType);
                         xe.SetAttribute("fullType", ep.ValueTypeFullName);
                         xe.SetAttribute("accessibility", ep.Accessibility);
                         xe.SetAttribute("returnType", ep.ReturnType);
                         xe.SetAttribute("attribute", ep.Attribute);


                         //如果是方法，则value是方法代码
                         if (ep.PropertyType == enumPropertyType.Method)
                         {
                             xe.SetAttribute("value", "");

                             XmlElement xeMethodCode = doc.CreateElement("Code");
                             xeMethodCode.InnerText = ep.Value;
                             xe.AppendChild(xeMethodCode);
                         }
                         else
                         {
                             xe.SetAttribute("value", ep.Value);
                         }

                         //如果是事件，则有eventargs
                         if (ep.PropertyType == enumPropertyType.Event)
                         {
                             xe.SetAttribute("eventArgs", ep.EventArgs);
                         }

                         //如果是自定义属性，则在其子节点中添加代码模板
                         if (ep.PropertyType == enumPropertyType.CustomProperty)
                         {
                             XmlNode _customPro = codeDoc.GetElementsByTagName("CodeCustomProperty")[0];
                             XmlNode _nodeCopy2 = doc.CreateNode(XmlNodeType.Element, _customPro.Name, "");
                             _nodeCopy2.InnerXml = _customPro.InnerXml;
                             xe.AppendChild(_nodeCopy2);
                         }

                         //如果是自定义事件，则在其子节点中添加代码模板
                         if (ep.PropertyType == enumPropertyType.CustomEvent)
                         {
                             XmlNode _customEvent = codeDoc.GetElementsByTagName("CodeCustomEvent")[0];
                             XmlNode _nodeCopy3 = doc.CreateNode(XmlNodeType.Element, _customEvent.Name, "");
                             _nodeCopy3.InnerXml = _customEvent.InnerXml;
                             xe.AppendChild(_nodeCopy3);
                         }

                         //如果存在继承列表
                         if (ep.PropertyType == enumPropertyType.Inherit || (ep.InheritList != null && ep.InheritList.Count > 0))
                         {
                             foreach (eInherit ei in ep.InheritList)
                             {
                                 XmlElement xeChild = doc.CreateElement(ei.Type.ToString());
                                 xeChild.SetAttribute("name", ei.Name);
                                 xe.AppendChild(xeChild);
                             }

                         }

                         //如果存在参数列表
                         if (ep.ParamList != null && ep.ParamList.Count > 0)
                         {
                             foreach (eMethodParam emp in ep.ParamList)
                             {
                                 XmlElement xeChild = doc.CreateElement("Parameter");

                                 xeChild.SetAttribute("name", emp.ParamName);
                                 xeChild.SetAttribute("valueType", "String");
                                 xeChild.SetAttribute("returnType", emp.DataType);
                                 xeChild.SetAttribute("outputType", emp.OutputType);

                                 xe.AppendChild(xeChild);
                             }
                         }
                     }
                     BaseControl.AppendChild(xe);
                 }

                 #endregion



                 #region 其他控件节点-基底节点的子节点，不含Method，Field

                 Dictionary<string, exFullInfoForBuildChild> dic2 = new Dictionary<string, exFullInfoForBuildChild>();

                 #region 普通控件
                 
                 
                 foreach (KeyValuePair<string, exFullInfoForBuildChild> kvpControl in dic)
                 {
                     if (kvpControl.Key.Equals("Document"))
                     {
                         continue;
                     }
                     if (kvpControl.Value.IsLayout)
                     {  
                         //空的Group排除
                         if (kvpControl.Value.LayoutType == enumLayoutTypeSimple.Group)
                         {
                             List<KeyValuePair<string, exFullInfoForBuildChild>> numChildGroup = (

                                                from   s in dic
                                                where  s.Value.ParentLayoutName == kvpControl.Value.BuildName
                                                select s
                                               
                                                ).ToList();

                             if (numChildGroup.Count == 0)
                             {
                                 continue;
                             }
                         }

                         //空的Item排除
                         if (kvpControl.Value.LayoutType == enumLayoutTypeSimple.Item && String.IsNullOrEmpty(kvpControl.Value.InControl))
                         {
                             continue;
                         }

                         dic2.Add(kvpControl.Key, kvpControl.Value);
                         continue;
                     }
                     exFullInfoForBuildChild controlEX = kvpControl.Value;
                     XmlElement xeControl = doc.CreateElement("Object");

                     xeControl.SetAttribute("name",controlEX.BuildName);
                     xeControl.SetAttribute("assembly",controlEX.ControlFullName);
                     xeControl.SetAttribute("type",controlEX.ControlName);
                     xeControl.SetAttribute("accessibility","private");
                     xeControl.SetAttribute("children","Controls");
                     

                     //添加基底节点的属性
                     SortHelper.SortList<eProperty>("PropertyType", "ASC", controlEX.PropertyList);//排序
                     foreach (eProperty ep in controlEX.PropertyList)
                     {
                         XmlElement xe = doc.CreateElement(ep.PropertyType.ToString());

                         xe.SetAttribute("name",ep.Name);
                         xe.SetAttribute("value",ep.Value);
                         xe.SetAttribute("valueType",ep.ValueType);
                         xe.SetAttribute("fullType",ep.ValueTypeFullName);
                         xe.SetAttribute("accessibility",String.IsNullOrEmpty(ep.Accessibility) ? "private" : ep.Accessibility);
                         xe.SetAttribute("returnType",ep.ReturnType);
                         xe.SetAttribute("attribute",ep.Attribute);
                         

                         if (ep.PropertyType == enumPropertyType.Event)
                         {
                             xe.SetAttribute("eventArgs",ep.EventArgs);
                         }

                         xeControl.AppendChild(xe);
                     }
                     BaseControl.AppendChild(xeControl);
                 }
                 #endregion


                 #region 布局控件

                 foreach (KeyValuePair<string, exFullInfoForBuildChild> kvp in dic2)
                 {
                     exFullInfoForBuildChild controlEX2 = kvp.Value;
                     XmlElement xeControl = doc.CreateElement("Object");

                     xeControl.SetAttribute("name",controlEX2.BuildName);
                     xeControl.SetAttribute("assembly",controlEX2.ControlFullName);
                     xeControl.SetAttribute("type",controlEX2.ControlName);
                     xeControl.SetAttribute("accessibility","private");

                     //XmlAttribute xaChildrenxeControl = doc.CreateAttribute("children");
                     //xaChildrenxeControl.Value = "Controls";
                     //xeControl.Attributes.Append(xaChildrenxeControl);

                     //添加基底节点的属性
                     SortHelper.SortList<eProperty>("PropertyType", "ASC", controlEX2.PropertyList);//排序
                     foreach (eProperty ep in controlEX2.PropertyList)
                     {
                         XmlElement xe = doc.CreateElement(ep.PropertyType.ToString());

                         xe.SetAttribute("name",ep.Name);
                         xe.SetAttribute("value",ep.Value);
                         xe.SetAttribute("valueType",ep.ValueType);
                         xe.SetAttribute("fullType",ep.ValueTypeFullName);
                         xe.SetAttribute("accessibility",String.IsNullOrEmpty(ep.Accessibility) ? "private" : ep.Accessibility);
                         xe.SetAttribute("returnType",ep.ReturnType);
                         xe.SetAttribute("attribute",ep.Attribute);

                         xeControl.AppendChild(xe);
                     }

                     //添加特有属性
                     switch(controlEX2.LayoutType)
                     {
                         case enumLayoutTypeSimple.Base:  
                            
                             XmlElement xeLay = doc.CreateElement("LayoutControl");

                             xeLay.SetAttribute("controls", "Add");

                             //XmlAttribute xeLaya = doc.CreateAttribute("controls");
                             //xeLaya.Value = "Add";
                             //xeLay.Attributes.Append(xeLaya);

                             StringBuilder ss1=new StringBuilder();
                             foreach (string s in controlEX2.ControlsList)
                             {
                                 ss1.Append(s);
                                 ss1.Append(",");
                             }

                             xeLay.InnerText = ss1.ToString().Substring(0, (ss1.ToString().Length - 1));
                             //xeLaya.InnerText = ss1.ToString().Substring(0, (ss1.ToString().Length - 1));

                             xeControl.AppendChild(xeLay);
                             break;
                        
                         case enumLayoutTypeSimple.Group:
                                     
                             XmlElement xeLay2 = doc.CreateElement("LayoutGroup");

                             xeLay2.SetAttribute("controls","AddRange");

                             //XmlAttribute xeLaya2 = doc.CreateAttribute("controls");
                             //xeLaya2.Value = "AddRange";
                             //xeLay2.Attributes.Append(xeLaya2);

                             //找到所有此Group的子节点
                             StringBuilder ss2=new StringBuilder();
                             List<KeyValuePair<string, exFullInfoForBuildChild>> groupList = (
                                           from p in dic2
                                           where p.Value.ParentLayoutName == controlEX2.BuildName
                                           select p).ToList();
                             foreach (KeyValuePair<string, exFullInfoForBuildChild> ex in groupList)
                             {
                                 ss2.Append(ex.Value.BuildName);
                                 ss2.Append(",");
                             }
                             xeLay2.InnerText = ss2.ToString().Substring(0, (ss2.ToString().Length - 1));

                             //xeLaya2.InnerText = ss2.ToString().Substring(0, (ss2.ToString().Length - 1));
                             xeControl.AppendChild(xeLay2);
                             break;

                          default: break;
                     }
                     
                     BaseControl.AppendChild(xeControl);
                 }

                 #endregion

                 #endregion

                 documentRoot.AppendChild(BaseControl);

                 doc.Save(path);
             }
             catch (Exception ex)
             {
                 throw ex;
             }
         }


         public Hashtable BuildCSharpCode(string path)
         {

             try
             {
                 //string path = PubConstant.ControlXmlTemplatePath;
                 //path = String.Format("{0}BuildTest.xml", path);

                 XmlDocument doc = new XmlDocument();
                 doc.Load(path);
                 StringBuilder sb = new StringBuilder();//.CS文件
                 StringBuilder sb2 = new StringBuilder();//.Design.CS文件

              

                 #region .CS文件===========================================================================
                
                 #region 文件头

                 XmlNodeList xnlHead1 = doc.GetElementsByTagName("Code1Head");
                 XmlNode xnBase = doc.SelectSingleNode("//Document//Object");
                 foreach (XmlNode xn in xnlHead1)
                 {
                     sb.Append(xn.InnerText);
                     switch (xn.Attributes["type"].Value)
                     {
                         case "using":
                             sb.Replace("#$using$#", "");
                             break;

                         case "namespace":
                             XmlNode xnNamespace = doc.GetElementsByTagName("Namespace")[0];
                             sb.Replace("#$namespace$#", xnNamespace.Attributes["value"].Value);
                             break;

                         case "classname":
                             sb.Replace("#$classname$#", xnBase.Attributes["name"].Value);
                             XmlNode xnInherit = doc.GetElementsByTagName("Inherit")[0];
                             StringBuilder inheritSB = new StringBuilder();
                             if (xnInherit != null && xnInherit.HasChildNodes)
                             {
                                 foreach (XmlNode xnInheritChild in xnInherit.ChildNodes)
                                 {
                                     if (xnInheritChild.Attributes["name"] != null && !String.IsNullOrEmpty(xnInheritChild.Attributes["name"].Value))
                                     {
                                         inheritSB.Append(", ");
                                         inheritSB.Append(xnInheritChild.Attributes["name"].Value);
                                     }
                                 }
                             }
                             sb.Replace("#$inheritname$#", ":" + xnBase.Attributes["type"].Value + inheritSB.ToString());
                             break;

                         case "function":
                             sb.Replace("#$classname$#", xnBase.Attributes["name"].Value);
                             break;
                     }
                 }

                 #endregion


                 #region .CS字段
                 // 格式
                 // #$attribute$#
                 // #$accessibility$# #$returnType$# #$name$# #$value$#;

                 sb.Append("\n");
                 sb.Append("\t\t#region 字段");
                 sb.Append("\n");

                 XmlNodeList xnlField = doc.GetElementsByTagName("Field");
                 XmlNode codeField = doc.GetElementsByTagName("CodeField")[0];
                 foreach (XmlNode xnField in xnlField)
                 {
                     StringBuilder sbField = new StringBuilder();
                     sbField.Append(codeField.InnerText);
                     sbField = sbField.Replace("#$accessibility$#", xnField.Attributes["accessibility"].Value);
                     sbField = sbField.Replace("#$returnType$#", xnField.Attributes["returnType"].Value);
                     sbField = sbField.Replace("#$name$#", xnField.Attributes["name"].Value);
                     sbField = sbField.Replace("#$value$#", String.IsNullOrEmpty(xnField.Attributes["value"].Value) ? "" : "=" + xnField.Attributes["value"].Value);
                     sbField = xnField.Attributes["attribute"] != null ? String.IsNullOrEmpty(xnField.Attributes["attribute"].Value) ? sbField.Replace("#$attribute$#", "") : sbField.Replace("#$attribute$#", xnField.Attributes["attribute"].Value) : sbField.Replace("#$attribute$#", "");
                     sb.Append(sbField);
                 }
                 sb.Append("\n");
                 sb.Append("\t\t#endregion");
                 sb.Append("\n");
                 sb.Append("\n");

                 #endregion


                 #region .CS自定义属性

                 // 格式
                 // #$attribute$#
                 // #$accessibility$# #$returnType$# #$name$# { get; set; }

                 sb.Append("\n");
                 sb.Append("\t\t#region 自定义属性");
                 sb.Append("\n");
                 sb.Append("\n");
                 XmlNodeList xnlCustomProperty = doc.GetElementsByTagName("CodeCustomProperty");

                 foreach (XmlNode xnCustomProperty in xnlCustomProperty)
                 {
                     StringBuilder sbCustomProperty = new StringBuilder();
                     sbCustomProperty.Append(xnCustomProperty.InnerText);
                     sbCustomProperty = sbCustomProperty.Replace("#$accessibility$#", xnCustomProperty.ParentNode.Attributes["accessibility"].Value);
                     sbCustomProperty = sbCustomProperty.Replace("#$returnType$#", xnCustomProperty.ParentNode.Attributes["returnType"].Value);
                     sbCustomProperty = sbCustomProperty.Replace("#$name$#", xnCustomProperty.ParentNode.Attributes["name"].Value);
                     if (xnCustomProperty.ParentNode.Attributes["attribute"] != null)
                     {
                         sbCustomProperty = String.IsNullOrEmpty(xnCustomProperty.ParentNode.Attributes["attribute"].Value) ? sbCustomProperty.Replace("#$attribute$#", "") : sbCustomProperty.Replace("#$attribute$#", xnCustomProperty.ParentNode.Attributes["attribute"].Value);
                     }
                     else
                     {
                         sbCustomProperty = sbCustomProperty.Replace("#$attribute$#", "");
                     }
                     sb.Append(sbCustomProperty);
                 }
                 sb.Append("\n");
                 sb.Append("\t\t#endregion");
                 sb.Append("\n");
                 sb.Append("\n");

                 #endregion


                 #region .CS方法
                 // 格式
                 // #$attribute$#
                 // #$accessibility$# #$returnType$# #$name$#(#$parameter$#)
                 //  {
                 //  }

                 sb.Append("\n");
                 sb.Append("\t\t#region 方法");
                 sb.Append("\n");

                 XmlNodeList xnlMethod = doc.GetElementsByTagName("Method");
                 XmlNode codeMethod = doc.GetElementsByTagName("CodeMethod")[0];
                 foreach (XmlNode xnMethod in xnlMethod)
                 {
                     StringBuilder sbMethod = new StringBuilder();
                     sbMethod.Append(codeMethod.InnerText);
                     sbMethod = sbMethod.Replace("#$accessibility$#", xnMethod.Attributes["accessibility"].Value);
                     sbMethod = sbMethod.Replace("#$returnType$#", xnMethod.Attributes["returnType"].Value);
                     sbMethod = sbMethod.Replace("#$name$#", xnMethod.Attributes["name"].Value);

                     XmlNode xnCode = xnMethod.SelectSingleNode("Code");
                     sbMethod = sbMethod.Replace("#$code$#", xnCode.InnerText);

                     XmlNodeList xnlParam = xnMethod.SelectNodes("Parameter");
                     StringBuilder sbParam = new StringBuilder();
                     if (xnlParam.Count > 1)
                     {
                         for (int i = 0; i < xnlParam.Count; i++)
                         {
                             if (xnlParam[i].Attributes["outputType"].Value == "none")
                             {
                                 sbParam.Append(xnlParam[i].Attributes["returnType"].Value);
                                 sbParam.Append(" ");
                                 sbParam.Append(xnlParam[i].Attributes["name"].Value);
                                 if (i < xnlParam.Count - 1)
                                 {
                                     sbParam.Append(",");
                                 }
                             }
                             else
                             {
                                 sbParam.Append(xnlParam[i].Attributes["outputType"].Value);
                                 sbParam.Append(" ");
                                 sbParam.Append(xnlParam[i].Attributes["returnType"].Value);
                                 sbParam.Append(" ");
                                 sbParam.Append(xnlParam[i].Attributes["name"].Value);
                                 if (i < xnlParam.Count - 1)
                                 {
                                     sbParam.Append(",");
                                 }
                             }

                         }
                     }

                     sbMethod = sbMethod.Replace("#$parameter$#", sbParam.ToString());

                     if (xnMethod.ParentNode.Attributes["attribute"] != null)
                     {
                         sbMethod = String.IsNullOrEmpty(xnMethod.Attributes["attribute"].Value) ? sbMethod.Replace("#$attribute$#", "") : sbMethod.Replace("#$attribute$#", xnMethod.Attributes["attribute"].Value);
                     }
                     else
                     {
                         sbMethod = sbMethod.Replace("#$attribute$#", "");
                     }
                     sb.Append(sbMethod);
                 }
                 sb.Append("\n");
                 sb.Append("\t\t#endregion");
                 sb.Append("\n");
                 sb.Append("\n");

                 #endregion


                 #region .CS控件事件
                 //格式
                 //#$attribute$#
                 //#$accessibility$# #$returnType$# #$controlname$#_#$name$#(object sender, #$eventargs$# e)
                 //{
                 //}	

                 sb.Append("\n");
                 sb.Append("\t\t#region 控件事件");
                 sb.Append("\n");

                 XmlNodeList xnlEvent = doc.GetElementsByTagName("Event");
                 XmlNode codeEvent = doc.GetElementsByTagName("CodeEvent")[0];
                 foreach (XmlNode xnEvent in xnlEvent)
                 {
                     StringBuilder sbEvent = new StringBuilder();
                     sbEvent.Append(codeEvent.InnerText);
                     sbEvent = sbEvent.Replace("#$accessibility$#", xnEvent.Attributes["accessibility"].Value);
                     sbEvent = sbEvent.Replace("#$returnType$#", xnEvent.Attributes["returnType"].Value);
                     sbEvent = sbEvent.Replace("#$controlname$#", xnEvent.ParentNode.Attributes["name"].Value);//父节点可以选中控件本身
                     sbEvent = sbEvent.Replace("#$name$#", xnEvent.Attributes["name"].Value);
                     sbEvent = sbEvent.Replace("#$eventargs$#", xnEvent.Attributes["eventArgs"].Value);
                     if (xnEvent.ParentNode.Attributes["attribute"] != null)
                     {
                         sbEvent = String.IsNullOrEmpty(xnEvent.Attributes["attribute"].Value) ? sbEvent.Replace("#$attribute$#", "") : sbEvent.Replace("#$attribute$#", xnEvent.Attributes["attribute"].Value);
                     }
                     else
                     {
                         sbEvent = sbEvent.Replace("#$attribute$#", "");
                     }
                     sb.Append(sbEvent);
                 }
                 sb.Append("\n");
                 sb.Append("\t\t#endregion");
                 sb.Append("\n");
                 sb.Append("\n");
                 #endregion
                 

                 #region .CS文件尾

                 XmlNode xnBottom1 = doc.GetElementsByTagName("Code1Bottom")[0];
                 sb.Append(xnBottom1.InnerText);

                 #endregion 


                 #endregion





                 #region .Design.CS文件=============================================================
                 

                 #region .Design.CS文件头

                 XmlNodeList xnlHead2 = doc.GetElementsByTagName("Code2Head");
                 foreach (XmlNode xn in xnlHead2)
                 {
                     switch (xn.Attributes["type"].Value)
                     {
                         case "namespace":
                             XmlNode xnNamespace = doc.GetElementsByTagName("Namespace")[0];
                             sb2.Append(xn.InnerText.Replace("#$namespace$#", xnNamespace.Attributes["value"].Value));
                             break;

                         case "classname":
                             sb2.Append(xn.InnerText.Replace("#$classname$#", xnBase.Attributes["name"].Value).Replace("#$inheritname$#", ":" + xnBase.Attributes["type"].Value));
                             break;

                         case "dispose":
                             sb2.Append(xn.InnerText.Replace("**", "&&").Replace("$", "<").Replace("%", ">"));
                             break;
                     }
                 }

                 #endregion


                 #region .Design.CS Initialize Componet()

                 XmlNode xnIniHead = doc.GetElementsByTagName("Code2InitializeHead")[0];

                 XmlNode xnIniNew = doc.GetElementsByTagName("Code2InitializeBodyNew")[0];

                 XmlNode xnIniNote = doc.GetElementsByTagName("Code2InitializeBodyNote")[0];

                 XmlNode xnIniProperty = doc.GetElementsByTagName("Code2InitializeBodyProperty")[0];

                 XmlNode xnIniBottom = doc.GetElementsByTagName("Code2InitializeBottom")[0];

                 XmlNode xnIniLayoutGroup = doc.GetElementsByTagName("CodeLayoutGroup")[0];
                 XmlNode xnIniLayoutGroupItem = doc.GetElementsByTagName("CodeLayoutGroupItem")[0];

                 XmlNode xnIniLayoutControl = doc.GetElementsByTagName("CodeLayoutControl")[0];


                 XmlNodeList xnlControls = doc.SelectNodes("//Document//Object//Object");


                 #region 头
                 //   #region 组件设计器生成的代码
                 //    /// $summary% 
                 //    /// 设计器支持所需的方法 - 不要
                 //    /// 使用代码编辑器修改此方法的内容。
                 //    /// $/summary%
                 //   private void InitializeComponent()
                 //    {

                 sb2.Append("\n");
                 sb2.Append(xnIniHead.InnerText.Replace("$", "<").Replace("%", ">"));
                 #endregion


                 #region 初始化

                 //this.#$ControlName$#=new #$ControlFullName$#(#$Component$#);

                 sb2.Append("\t\t\tthis.components = new System.ComponentModel.Container();\n");
                 foreach (XmlNode xnControl in xnlControls)
                 {
                     sb2.Append(xnIniNew.InnerText);
                     sb2 = sb2.Replace("#$ControlName$#", xnControl.Attributes["name"].Value);
                     sb2 = sb2.Replace("#$ControlFullName$#", xnControl.Attributes["assembly"].Value.Split(',')[0]);
                     sb2 = sb2.Replace("#$Component$#", "");
                 }
                 #endregion


                 #region 控件属性


                 sb2.Append("\n");
                 foreach (XmlNode xnControl in xnlControls)
                 {
                     //注释---------------------------------------------------------------
                     //   ///
                     //   ///#$name$#
                     //   ///

                     sb2.Append(xnIniNote.InnerText.Replace("#$name$#", xnControl.Attributes["name"].Value));



                     //属性----------------------------------------------------------------
                     //this#$ControlName$#.#$PropertyName$#=#$PropertyValue$#;(this后面没有点，要自己加上)

                     XmlNodeList xnlControlPro = xnControl.ChildNodes;
                     foreach (XmlNode xnControlPro in xnlControlPro)
                     {

                         if (xnControlPro.Name == "Property")//属性
                         {
                             sb2.Append(xnIniProperty.InnerText);
                             sb2 = sb2.Replace("#$ControlName$#", "." + xnControl.Attributes["name"].Value);
                             sb2 = sb2.Replace("#$PropertyName$#", xnControlPro.Attributes["name"].Value);
                             sb2 = sb2.Replace("#$PropertyValue$#", String.Format(GetCodeFromTypes.GetCode(xnControlPro.Attributes["fullType"].Value), xnControlPro.Attributes["value"].Value));
                         }
                         else if (xnControlPro.Name == "Event")//事件
                         {
                             sb2.Append(xnIniProperty.InnerText);
                             sb2 = sb2.Replace("#$ControlName$#", "." + xnControl.Attributes["name"].Value);
                             sb2 = sb2.Replace("#$PropertyName$#", xnControlPro.Attributes["name"].Value + " +");
                             sb2 = sb2.Replace("#$PropertyValue$#", String.Format(GetCodeFromTypes.GetCode(xnControlPro.Attributes["eventArgs"].Value), xnControlPro.Attributes["value"].Value));
                         }

                         //this.#$ControlName$#.Items.AddRange(new DevExpress.XtraLayout.BaseLayoutItem[]{
                         else if (xnControlPro.Name == "LayoutGroup")//布局组
                         {
                             sb2.Append(xnIniLayoutGroup.InnerText);
                             sb2 = sb2.Replace("#$ControlName$#", xnControl.Attributes["name"].Value);
                             for (int i = 0; i < xnControlPro.InnerText.Split(',').Length; i++)
                             {
                                 sb2.Append(xnIniLayoutGroupItem.InnerText);
                                 if (i == xnControlPro.InnerText.Split(',').Length - 1)
                                 {
                                     sb2 = sb2.Replace("#$ItemName$#", xnControlPro.InnerText.Split(',')[i] + "});");//最后一个不要逗号加上括号
                                 }
                                 else
                                 {
                                     sb2 = sb2.Replace("#$ItemName$#", xnControlPro.InnerText.Split(',')[i] + ",");
                                 }
                             }

                         }

                         //this.#$ControlName$#.Controls.Add(this.#$ItemName$#);
                         else if (xnControlPro.Name == "LayoutControl")//布局基底
                         {
                             for (int i = 0; i < xnControlPro.InnerText.Split(',').Length; i++)
                             {
                                 sb2.Append(xnIniLayoutControl.InnerText);
                                 sb2 = sb2.Replace("#$ControlName$#", xnControl.Attributes["name"].Value);
                                 sb2 = sb2.Replace("#$ItemName$#", xnControlPro.InnerText.Split(',')[i]);
                             }
                         }

                     }
                 }
                 #endregion


                 #region 基底属性


                 //注释---------------------------------------------------------------
                 //   ///
                 //   ///#$name$#
                 //   ///

                 sb2.Append(xnIniNote.InnerText.Replace("#$name$#", xnBase.Attributes["name"].Value));


                 //属性----------------------------------------------------------------
                 //this#$ControlName$#.#$PropertyName$#=#$PropertyValue$#;(this后面没有点，要自己加上)

                 foreach (XmlNode xnBaseChild in xnBase)
                 {
                     if (xnBaseChild.Name == "Property")
                     {
                         sb2.Append(xnIniProperty.InnerText);
                         sb2 = sb2.Replace("#$ControlName$#", "");
                         sb2 = sb2.Replace("#$PropertyName$#", xnBaseChild.Attributes["name"].Value);
                         sb2 = sb2.Replace("#$PropertyValue$#", String.Format(GetCodeFromTypes.GetCode(xnBaseChild.Attributes["fullType"].Value), xnBaseChild.Attributes["value"].Value));
                     }
                     if (xnBaseChild.Name == "Event")
                     {
                         sb2.Append(xnIniProperty.InnerText);
                         sb2 = sb2.Replace("#$ControlName$#", "");
                         sb2 = sb2.Replace("#$PropertyName$#", xnBaseChild.Attributes["name"].Value + "+");
                         sb2 = sb2.Replace("#$PropertyValue$#", String.Format(GetCodeFromTypes.GetCode(xnBaseChild.Attributes["eventArgs"].Value), xnBaseChild.Attributes["value"].Value));
                     }

                     //this.#$ControlName$#.Controls.Add(this.#$ItemName$#);
                     if (xnBaseChild.Name == "Data")
                     {
                         if (xnBaseChild.Attributes["Type"].Value == "Controls")
                         {
                             for (int i = 0; i < xnBaseChild.InnerText.Split(',').Length; i++)
                             {
                                 sb2.Append(xnIniLayoutControl.InnerText);
                                 sb2 = sb2.Replace("#$ControlName$#", "#$_$#");
                                 sb2 = sb2.Replace(".#$_$#.", ".");
                                 sb2 = sb2.Replace("#$ItemName$#", xnBaseChild.InnerText.Split(',')[i]);
                             }
                         }
                     }
                 }


                 #endregion


                 #region 尾
                 //   }
                 // #endregion

                 sb2.Append("\n");
                 sb2.Append(xnIniBottom.InnerText);
                 #endregion

                 #endregion


                 #region .Design.CS控件定义

                 //格式
                 //#$accessibility$# #$fullName$# #$name$#;

                 sb2.Append("\n");
                 XmlNode xnContronD = doc.GetElementsByTagName("Code2ControlDefine")[0];
                 foreach (XmlNode xnControlDefine in xnlControls)
                 {
                     StringBuilder sbControlDefine = new StringBuilder();
                     sbControlDefine.Append(xnContronD.InnerText);
                     sbControlDefine = sbControlDefine.Replace("#$accessibility$#", xnControlDefine.Attributes["accessibility"].Value);
                     sbControlDefine = sbControlDefine.Replace("#$fullName$#", xnControlDefine.Attributes["assembly"].Value.Split(',')[0]);
                     sbControlDefine = sbControlDefine.Replace("#$name$#", xnControlDefine.Attributes["name"].Value);
                     sb2.Append(sbControlDefine);
                 }
                 sb2.Append("\n");


                 #endregion


                 #region .Design.CS文件尾

                 XmlNode xnBottom2 = doc.GetElementsByTagName("Code2Bottom")[0];
                 sb2.Append(xnBottom2.InnerText);

                 #endregion 

                 
                 #endregion


                 /*-------------------------------------------------------------------------------------------*/

                 Hashtable ht = new Hashtable();
                 ht.Add("Code1", sb);
                 ht.Add("Code2", sb2);
                 return ht;

             }
             catch (Exception ex)
             {
                 throw ex;
             }
         }
    }
}
